package com.msun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.msun.constant.MessageConstant;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.entity.Result;
import com.msun.pojo.Setmeal;
import com.msun.service.CheckGroupService;
import com.msun.service.OrderListService;
import com.msun.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderlist")
public class OrderListController {

    @Reference
    private OrderListService orderListService;
    @Reference
    private SetmealService setmealService;
    @Reference
    private CheckGroupService checkGroupService;
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        String[] selectDate = queryPageBean.getSelectDate();
        PageResult pageResult = orderListService.pageQuery(
                selectDate,
                queryPageBean.getSelectStatus(),
                queryPageBean.getSelectType(),
                queryPageBean.getQueryString(),
                queryPageBean.getPageSize(),
                queryPageBean.getCurrentPage());
        return pageResult;
    }
    //分页查询已到诊的数据
    @RequestMapping("/findPage1")
    public PageResult findPage1(@RequestBody QueryPageBean queryPageBean) {
        String[] selectDate = queryPageBean.getSelectDate();
        PageResult pageResult = orderListService.pageQuery(
                selectDate,
                "已到诊",
                queryPageBean.getSelectType(),
                queryPageBean.getQueryString(),
                queryPageBean.getPageSize(),
                queryPageBean.getCurrentPage());
        return pageResult;
    }

    /**
     * 获取套餐详情
     * @param setMealId
     * @return
     */
    @RequestMapping("/detalSetmeal/{setMealId}")
    public Result detalSetmeal(@PathVariable Integer setMealId) {
        try {
            // 通过order获取套餐id,在通过套餐id查询详细的套餐
            List<String> list = checkGroupService.fingBySetmaelId(setMealId);
            List<Map> ll = new ArrayList<>();
            for (String s : list) {
                Map<String, String> map = new HashMap<>();
                map.put("name",s);
                map.put("result","正常");
                ll.add(map);
            }
            return new Result(true,"成功",ll);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }


    //开关修改状态
    @RequestMapping("/changeStatus")
    public Result chanStatus(@RequestBody Map<String, Object> map) {
        try {
            orderListService.changeStatus(map);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //删除预约信息
    @RequestMapping("/delete")
    public Result delete(Integer orderId){
        System.out.println();
        try {
            orderListService.delete(orderId);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"无法删除");
        }

    }
    //添加预约信息
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> param) {
        try {
            orderListService.add(param);
        } catch (RuntimeException e) {
//            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "预约订单添加失败");
        }
        return new Result(true, "预约订单添加成功");
    }

    //预约信息修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map){
        try {
            orderListService.edit(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"预约信息修改失败");
        }
       return  new Result(true,"预约信息修改成功");
    }
}